﻿using Sys.Common;
using Sys.Core.Gen;
using Sys.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Sys.Api.Gen
{
    public class FocuApi : BaseApi
    {
        public string Grid(int rows, int page)
        {
            var entities = FocuBll.I.SortEntities;

            var list = FocuBll.I.EntitiesPage(entities, rows, page);

            return GetGrid(entities.Count(), list);
        }

        public string Save(GenFocu model)
        {
            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                model.IntSort = FocuBll.I.MaxSort(m => m.IntSort);

                result.Success = FocuBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                result.Success = FocuBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = FocuBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string SetOrder(string id, string group, string order)
        {
            JsonMsg result = new JsonMsg();

            var entities = FocuBll.I.Entities.Where(m => m.Group.Equals(group));

            bool success = false;

            result.Message = FocuBll.I.SetOrder(entities, id, "IntSort", order, out success);

            result.Success = success;

            return JsonHelper.ToJson(result);
        }

        public string EnableOrDisable(string id, bool bit)
        {
            JsonMsg result = new JsonMsg();

            GenFocu model = FocuBll.I.GetByKey(id);

            if (model != null)
            {
                if (model.IsVisible != bit)
                {
                    model.IsVisible = bit;

                    result.Success = FocuBll.I.Update(model) > 0;

                    if (result.Success)
                    {
                        result.Message = bit ? "启用成功!" : "禁用成功!";
                    }
                }
                else
                {
                    result.Message = bit ? "已经是启用状态!" : "已经是禁用状态!";
                }
            }

            return JsonHelper.ToJson(result);
        }

        public string AddImg()
        {
            Hashtable hash = KindHelper.ImgUpload();

            if (Convert.ToInt32(hash["error"]) == 0)
            {
                HttpContext context = HttpContext.Current;

                int w = Convert.ToInt32(context.Request.QueryString["w"]);
                int h = Convert.ToInt32(context.Request.QueryString["h"]);

                string filename = hash["url"].ToString();

                string temppath = context.Server.MapPath("~/userfiles/temp/" + filename);
                string filepath = context.Server.MapPath("~/userfiles/focu/");

                if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); }

                ThumNail.MakeThumNail(temppath, filepath + filename, w, h, TnType.Cut);

                System.IO.File.Delete(temppath);

                hash["url"] = filename;
            }

            return JsonHelper.ToJson(hash);
        }
    }
}
